#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <linux/limits.h>
#include <string.h>
#include "list.h"
#include "debug.h"
// include assert.h instead of using #define ASSERT_TRUE(x)
// ASSERT_FUNC(function call, expected value)
// it might be better if I add handler separately...
#define ASSERT_FUNC(x, y)   if (x != y) {                               \
                                printf("error occurred in %s\n", #x);   \
                                return EXIT_FAILURE;                    \
                            }
#define CSCOPE_FILES        "cscope.files"

typedef struct fileList_t {
    char                fileName[PATH_MAX];
    struct list_head    list;
} fileList;




int get_file_lists(struct list_head *listHead, const char *filePath)
{
    return EXIT_SUCCESS;
}

/*
 * 1. read file
 * 2. attach html tag
 * 3. write src_file_name.html
 */
int generate_html(const char* srcFile)
{
    FILE *fpSrcFile;
    FILE *fpHtmlFile;
    char htmlFilePath[PATH_MAX];

    // 1. read file
    strcpy(htmlFilePath, srcFile);
    strcat(htmlFilePath, ".html");
    C2W_PRINT("%s\n", htmlFilePath);

    fpSrcFile = fopen(srcFile, "r");
    if (fpSrcFile == NULL)
    {
        C2W_PRINT("fopen(%s) failed\n", srcFile);
        return EXIT_FAILURE;
    }

    fpHtmlFile = fopen(htmlFilePath, "w");
    if (fpHtmlFile == NULL)
    {
        C2W_PRINT("fopen(%s) failed\n", htmlFilePath);
        fclose(fpSrcFile);
        return EXIT_FAILURE;
    }

    // 2. attach html tag
    char aTag[BUFSIZ] = {'\0',};
    char reservLine[BUFSIZ] = {'\0',};
    char *spChar;

    strcpy(aTag, "<html>\n");
    fwrite(aTag, strlen(aTag), 1, fpHtmlFile);
    memset(aTag, '\0', BUFSIZ);

    while(fgets(aTag, BUFSIZ, fpSrcFile)) {
        //This is just an example to see what to do with special letter in html
        //need more of this
        while((spChar = strchr(aTag, '<')) != NULL) {
            strcpy(reservLine, spChar);
            strcpy(spChar, "&lt;");
            strcpy(spChar + 4, reservLine + 1);
        }
        memset(reservLine, '\0', BUFSIZ);
        while((spChar = strchr(aTag, '>')) != NULL) {
            strcpy(reservLine, spChar);
            strcpy(spChar, "&gt;");
            strcpy(spChar + 4, reservLine + 1);
        }
        memset(reservLine, '\0', BUFSIZ);
        while((spChar = strchr(aTag, ' ')) != NULL) {
            strcpy(reservLine, spChar);
            strcpy(spChar, "&nbsp;");
            strcpy(spChar + 6, reservLine + 1);
        }

    //    printf("[%s", aTag);
        strcat(aTag, "<br>");
        fwrite(aTag, strlen(aTag), 1, fpHtmlFile);
    }
    memset(aTag, '\0', BUFSIZ);

    strcpy(aTag, "</html>");
    fwrite(aTag, strlen(aTag), 1, fpHtmlFile);
    memset(aTag, '\0', BUFSIZ);

    // 3. write src_file_name.html
    fclose(fpSrcFile);
    fclose(fpHtmlFile);

    return EXIT_SUCCESS;
}

// 1.   get file lists
// 2.   for each file list
// 2.1      read file
// 2.2      attach html tags
// 2.3      generate src_file_name.html
// 3.   end?
int generate_web(const char *cscopeOutFilePath)
{
    char        *cscopeFilePath;

    cscopeFilePath = getcwd((char*)cscopeOutFilePath, PATH_MAX);

    LIST_HEAD(srcFiles); //head created

    C2W_PRINT("list head initialized\n");

#if 1 //TODO: use function call
    char fileListFile[PATH_MAX] = {'\0', };
    FILE *pfFileList;
    char listEntry[PATH_MAX] = {'\0',};

    sprintf(fileListFile, "%s/%s", cscopeFilePath, CSCOPE_FILES);
    printf("Getting file list from \"%s\"\n", fileListFile);

    pfFileList = fopen(fileListFile, "r");

    fileList *flist;

    while(fgets(listEntry, PATH_MAX, pfFileList)) {
        flist = malloc(sizeof(fileList));
        listEntry[strlen(listEntry) - 1] = '\0'; //get rid of newline
        strcpy(flist->fileName, listEntry);
        list_add(&flist->list, &srcFiles);
        C2W_PRINT("%s\n", listEntry);
    }
    fclose(pfFileList);
#else
    ASSERT_FUNC(get_file_lists(&srcFiles, cscopeFilePath), 0);
#endif

    fileList *pos = NULL;

    list_for_each_entry_reverse(pos, &srcFiles, list)
    {
        //C2W_PRINT("fileList[%d] = %s", i++, pos->fileName);
        ASSERT_FUNC(generate_html(pos->fileName), 0);
    }

    return EXIT_SUCCESS;
}
